<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            停机提报
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">设备编码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描设备编码" id="equipCode" value="" onclick="getIdSelector();" />
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">产线</div>
                    <input type="text" class="aui-input"  id="lineCode" value="" disabled>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">工位</div>
                    <input type="text" class="aui-input"  id="workCellCode" value="" disabled>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width:20%;">开始时间</div>
                    <div class="aui-list-item-input" style="float:left;width: 75%;">
                        <input type="text" id="startTime" onclick="showTime();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width:20%;">结束时间</div>
                    <div class="aui-list-item-input" style="float:left;width: 75%;">
                        <input type="text" id="endTime" onclick="showTime2();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">停机大类</div>
                    <div class="aui-list-item-input">
                        <select onchange="prodShutdownTypeChange()" id="prodShutdownType">
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">停机原因</div>
                    <div class="aui-list-item-input">
                        <select onchange="typeClassList()" id="typeClass">
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">停机内容</div>
                    <div class="aui-list-item-input">
                        <select id="resonName">
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">备注</div>
                    <div class="aui-list-item-input">
                        <input class="form-control" type="text" id="ext10">
                    </div>
                </div>
            </li>

        </ul>
        <p></p>
        <p></p>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">保存</div>

    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/jdate.min.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var equipCode = ""; //设备编码

    var lineCode = "";//产线编码
    var lineName = "";//产线名称
    var workCellCode = "";//工位编码
    var workCellName = "";//工位名称

    var ss = [];


    //初始化查询仓库下拉框
    apiready = function () {
        $("#equipCode").focus();
        idSelector = "equipCode";
        //查询停机大类下拉框
        doSearchExt03();
    }

    //7；PDA自带扫描回车事件-设备编码
    $("#equipCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //设备编码
            $("#sm").blur();
            $("#equipCode").val(sm);
            getEquipMsg()
        }
    });


    //查询设备信息
    function getEquipMsg() {
        var equipCode = $("#equipCode").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getEquipMsg";
        var data = {
            values: {
                equipCode: equipCode,
                un: un,
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {
                    lineCode = myobj.lineCode;
                    lineName = myobj.lineName;
                    workCellCode = myobj.workCellCode;
                    workCellName = myobj.workCellName;

                    $("#lineCode").val(lineCode + ':' + lineName);
                    $("#workCellCode").val(workCellCode + ':' + workCellName);

                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                    lineCode = "";
                    lineName = "";
                    workCellCode = "";
                    workCellName = "";
                    $("#lineCode").val("");
                    $("#workCellCode").val("");
                    $("#equipCode").val("");
                    $("#equipCode").focus();
                    idSelector = "equipCode";
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }


    function showTime() {
        var moduleTimer = api.require('moduleTimer');
        moduleTimer.showTimer({
            startTime: '2000-01-1 00:00',//可选择的起始时间
            endTime: '2099-12-31 23:59',//可选择的结束时间
            timeFormat: 'yyyy-MM-dd HH:mm:ss',//返回的时间格式，如果不传默认是yyyy-MM-dd HH:mm:ss
            selColor: '#079962'//选中的颜色值
        }, function (ret, err) {
            var startTime = ret;
            var text = document.getElementById("startTime");
            text.value = startTime;
        });
    }

    function showTime2() {
        var moduleTimer = api.require('moduleTimer');
        moduleTimer.showTimer({
            startTime: '2000-01-1 00:00',//可选择的起始时间
            endTime: '2099-12-31 23:59',//可选择的结束时间
            timeFormat: 'yyyy-MM-dd HH:mm:ss',//返回的时间格式，如果不传默认是yyyy-MM-dd HH:mm:ss
            selColor: '#079962'//选中的颜色值
        }, function (ret, err) {
            var endTime = ret;
            var text = document.getElementById("endTime");
            text.value = endTime;
        });
    }


    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    function typeClassList() {
        // 获取停机原因的value
        var value = $("#typeClass").val();
        var un = localStorage.getItem('un');
        console.log("！！！！！！", JSON.stringify(value))
        var shutdownid = ''
        console.log("@@@@@@@", JSON.stringify(ss))
        for (var i = 0; i < ss.length; i++) {
            console.log("Label:", ss[i].label);
            console.log("Value:", ss[i].value);
            if (value == ss[i].value) {
                shutdownid = ss[i].sysid;
                console.log("shutdownid:", shutdownid);
            }
        }
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/comboxList";
        var data = {
            values: { // 以表单方式提交参数（JSON对象）
                tablename: 'md_shutdown_detail',
                column_name: 'name',
                column_value: 'code',
                condition: 'is_del = 0 and md_shutdown_type_id = ' + "'" + shutdownid + "'",
                un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, (ret, err) => { // 使用箭头函数确保 this 指向正确
            if (ret) {
                var map = ret;
                var list = map.list || [];
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("resonName"), html);
            } else {
                console.error("API error: ", err);
            }
        });
    }
    function prodShutdownTypeChange() {
        // 获取停机大类的值
        var a = $("#prodShutdownType").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/comboxList";
        var data = {
            values: { // 以表单方式提交参数（JSON对象）
                tablename: 'md_shutdown_type',
                column_name: 'name',
                column_value: 'code',
                other_column: 'sysid',
                condition: 'is_del=0 and type = ' + "'" + a + "'",
                un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, (ret, err) => { // 使用箭头函数确保 this 指向正确
            if (ret) {
                var data = JSON.stringify(ret);
                console.log(data)
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                console.log("mmmmmmmmmmmmmm" + JSON.stringify(list))
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("typeClass"), html);
                ss = eval(map.list);
                console.log("$$$$$$$$$$$" + JSON.stringify(ss))
                // 调用停机内容下拉框的异步操作
                this.typeClassList();
            } else {
                console.error("API error: ", err);
            }
        });
    }
    //停机大类下拉框
    function doSearchExt03() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/comboxListBycodeType";
        var data = {
            values: { // 以表单方式提交参数（JSON对象）
                code: 'SHUTDOWN_TYPE',
                removeDefault: 'Y',
                un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data,
        }, (ret, err) => { // 使用箭头函数绑定 this
            if (ret) {
                var map = ret;
                var list = map.list || [];
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    if (list[i].value == "1") {
                        continue;
                    }
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("prodShutdownType"), html);
                // 查询停机原因下拉框
                this.prodShutdownTypeChange(); // 确保在回调内调用
            } else {
                console.error("API error: ", err);
            }
        });
    }
    function resetData() {
        //开始时间
        $("#startTime").val("");
        //结束时间
        $("#endTime").val("");
        equipCode = ""; //设备编码

        lineCode = "";//产线编码

        lineName = "";//产线名称
        workCellCode = "";//工位编码
        workCellName = "";//工位名称
        $("#lineCode").val("");
        $("#workCellCode").val("");
        //设备编码
        $("#equipCode").val("");
        //停机大类
        $("#prodShutdownType").val("");
        //停机原因
        $("#typeClass").val("");
        //停机内容
        $("#resonName").val("");
        //备注
        $("#ext10").val("");
        //光标设置
        $("#equipCode").focus();
        idSelector = "equipCode";
    }
    //保存按钮
    function save() {
        //设备编码
        var equipCode = $("#equipCode").val();
        //设备非空校验
        if (equipCode == "" || equipCode == null || equipCode == undefined) {
            api.toast({
                msg: '请选择/扫描设备',
                duration: 3000
            });
            return;
        }
        //开始时间
        var startTime = $("#startTime").val();
        //开始时间非空校验
        if (startTime == "" || startTime == null || startTime == undefined) {
            api.toast({
                msg: '请选择开始时间',
                duration: 3000
            });
            return;
        }

        //结束时间
        var endTime = $("#endTime").val();

        //结束时间非空校验
        if (endTime == "" || endTime == null || endTime == undefined) {
            api.toast({
                msg: '请选择结束时间',
                duration: 3000
            });
            return;
        }


        //停机大类
        var prodShutdownType = $("#prodShutdownType").val();
        //停机原因
        var typeClass = $("#typeClass").val();
        //停机内容
        var resonName = $("#resonName").val();
        //备注
        var ext10 = $("#ext10").val();

        //设备非空校验
        if (equipCode == "" || equipCode == null || equipCode == undefined) {
            api.toast({
                msg: '请选择/扫描设备',
                duration: 3000
            });
            return;
        }

        //停机大类
        if (prodShutdownType == "" || prodShutdownType == null || prodShutdownType == undefined) {
            api.toast({
                msg: '请选择停机大类',
                duration: 3000
            });
            return;
        }
        //停机原因
        if (typeClass == "" || typeClass == null || typeClass == undefined) {
            api.toast({
                msg: '请选择停机原因',
                duration: 3000
            });
            return;
        }
        //停机内容
        if (resonName == "" || resonName == null || resonName == undefined) {
            api.toast({
                msg: '请选择停机内容',
                duration: 3000
            });
            return;
        }



        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un'); //登录用户
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/emequipmentAdd";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                startTime: startTime,//开始时间
                endTime: endTime,//结束时间时间
                lineCode: lineCode,//产线编码
                lineName: lineName,//产线名称
                workCellCode: workCellCode,//工位编码
                workCellName: workCellName,//工位名称
                prodShutdownType: prodShutdownType, // 停机大类
                typeClass: typeClass, //停机原因
                resonName: resonName, //停机内容
                equipCode: equipCode, //设备编码
                ext10: ext10, //备注
                un: un //登录用户
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, (ret, err) => { // 使用箭头函数绑定 this
          
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                console.log(myobj);
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                    //清空数据
                    this.resetData()
                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }




    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>